package com.javasm.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @className: RequestBodyUtil
 * @description
 * @author: gfs
 * @date: 2024/7/25 10:54
 * @version: 0.1
 * @since : jdk11
 */
public class RequestBodyUtil {

    //处理 request中的参数,转成 json字符串返回
    public static String read(HttpServletRequest request) throws IOException {
        //request对象的流信息
        ServletInputStream inputStream = request.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        //把字节流转成了 字符串流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        //StringBuilder 存储参数
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine())!=null){
            sb.append(line).append("\n");
        }
        //把StringBuilder转成String字符串
        String json = sb.toString().trim();
        return json;
    }
}
